package com.bbc.fragment;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bbc.R;
import com.bbc.base.BaseFragment;
import com.bbc.ui.WaveDemoActivity;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.AsyncSubject;

/**
 * Created by Administrator on 2017/9/26.
 */

public class PersonFragment extends BaseFragment {

    @BindView(R.id.iv_first_qid)
    ImageView mIvFirstQid;
    private static final int ANIM_TIME = 3000;
    private static final int[] Imgs = {
            R.drawable.bg, R.drawable.bg,
            R.drawable.bg, R.drawable.personalbng,
            R.drawable.bg, R.drawable.personalbng,
            R.drawable.bg, R.drawable.personalbng,
            R.drawable.bg, R.drawable.personalbng,
            R.drawable.message_on, R.drawable.home_on,};

    @Override
    protected void setListeners() {

    }

    @Override
    protected void initData() {
        Random random = new Random(SystemClock.elapsedRealtime());//SystemClock.elapsedRealtime() 从开机到现在的毫秒数（手机睡眠(sleep)的时间也包括在内）
        mIvFirstQid.setImageResource(Imgs[random.nextInt(Imgs.length)]);
        Observable.timer(2000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {
                        startAnim();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    private void startAnim() {

        ObjectAnimator animatorX = ObjectAnimator.ofFloat(mIvFirstQid, "scaleX", 2f, 1.15f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(mIvFirstQid, "scaleY", 2f, 1.15f);

        AnimatorSet set = new AnimatorSet();
        set.setDuration(ANIM_TIME).play(animatorX).with(animatorY);
        set.start();

        set.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                getContext().startActivity(new Intent(getContext(), WaveDemoActivity.class));
                getActivity().finish();
            }
        });
    }

    @Override
    protected void findViewId() {

    }

    @Override
    protected int getViewLayou() {
        return R.layout.test_fragment;
    }


}
